package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.AddressList;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.OftenPlaceView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by ukaodev on 2017/6/26.
 */

public class OftenPlacePesenter extends BasePresenter<OftenPlaceView> {

    public OftenPlacePesenter(OftenPlaceView view) {
        attachView(view);
    }






    public void getAddressRequest(Context mContext, String  useid) {
       mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("userId", useid);
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.userAddress(Constant.createParameter(mHashMap)),
                new ApiCallback<AddressList>() {
                    @Override
                    public void onSuccess(AddressList model) {

                        if (model.getHttpCode() == 200) {
                            List<AddressList> mAddressList=  model.getData();
                            mvpView.loadAddressSuccess(mAddressList);

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }




                    @Override
                    public void onFinish() {
                    mvpView.hideLoading();
                    }
                });
    }

    public void addressDelete(final AddressList  item) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", item.getId());
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.addressDelete(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {

                            mvpView.deleteSuccess(item,model.getMsg());

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }




                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
    /**
     设为默认
     */
    public void addressDefault(final AddressList       defaultitem,final int  pos  ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", defaultitem.getId());
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.updateDefault(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        mvpView.showLoading();
                        if (model.getHttpCode() == 200) {

                            mvpView.addressDefault(model.getMsg(),defaultitem,pos);

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }




                    @Override
                    public void onFinish() {
                    mvpView.hideLoading();
                    }
                });
    }




}
